/*
 * rewarn.h
 *
 *  Created on: 2009-5-25
 *      Author: tulai
 */

#ifndef REWARN_H_
#define REWARN_H_

#include "rewarn_nlist.h"
#include "rewarn_pkt.h"

#include <cmu-trace.h>
#include <priqueue.h>
#include <classifier/classifier-port.h>
#include <common/mobilenode.h>



class rewarn;

/*
  Timers (RewarnTimer, RelayTimer)
*/
class RewarnTimer : public Handler {
public:
        RewarnTimer(rewarn* a) : agent(a) {}
        void    handle(Event*);
        void          BroadcastExpire();
        void          SetRewarnTimer();

private:
        rewarn    *agent;
        Event   intr;
};

class RelayTimer : public Handler {
public:
        RelayTimer(rewarn* a) : agent(a) {}
        void    handle(Event*);

        void          RelayExpire();
        void          SetRelayTimer(nsaddr_t n1, nsaddr_t n2, double abstime);
        void          KillRelayTimer(nsaddr_t n1, nsaddr_t n2);

private:
        rewarn    *agent;
        Event   intr;
};

class rewarn: public Agent {

  // Friends
  friend class RewarnTimer;
  friend class RelayTimer;

public:
  rewarn(nsaddr_t id);
  void          recv(Packet *p, Handler *);
  void          recvReWarn(Packet *p);
  void          recvRwRelay(Packet *p);
  void          sendReWarn();
  void          sendRwRelay(nsaddr_t n1, nsaddr_t n2);

  double        getttx(node_state_t *n1, node_state_t *n2);
  void          sndWarning();

  double        dist(double x1, double y1, double x2, double y2);

  void          log_rewarn_pkt();
  void          log_rwrelay();



  u_int16_t     WarningCounter;
  node_state_t lstate_;
  rewarn_nlist* nlist_;
  rewarn_rlist* rlist_;
  Trace*        logtarget;
  char filename[256];



  /*
  float                 vector(float vec[200][4]);
  float                 getvector(float getvec[200][4]);
  void                  getcour();
  void                  CA_app_send();
  void                  CA_app_recv();
  void                  CPA_Calculation(float node[2][4], int target_ID);
*/


protected:
       int             command(int, const char *const *);


       /*
        * Timers
        */
       RewarnTimer  btimer;
       RelayTimer      rtimer;


       nsaddr_t ra_addr_;
       MobileNode* node_;



  /* for passing packets up to agents */
       PortClassifier *dmux_;

};



#endif /* REWARN_H_ */
